package com.bobochang.warehouse.controller;

import com.bobochang.warehouse.annotation.BusLog;
import com.bobochang.warehouse.constants.WarehouseConstants;
import com.bobochang.warehouse.dto.AssignRoleDto;
import com.bobochang.warehouse.entity.*;
import com.bobochang.warehouse.mapper.UserMapper;
import com.bobochang.warehouse.page.Page;
import com.bobochang.warehouse.service.AuthService;
import com.bobochang.warehouse.service.DepartmentService;
import com.bobochang.warehouse.service.RoleService;
import com.bobochang.warehouse.service.UserInfoService;
import com.bobochang.warehouse.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 2023/7/12 - 10:43
 *
 * @author bobochang
 * @description
 */
@RequestMapping("/Department")
@RestController
@Transactional
@BusLog(name = "部门管理")
@Slf4j
public class DepartmentController {

    @Autowired
    private AuthService authService;
    @Autowired
    private UserInfoService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DepartmentService departmentService;



    @PostMapping("/addDepartment")
    @BusLog(descrip = "添加部门")
    public Result addUser(@RequestBody Department department,
                          @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即创建新用户的用户id
        int createBy = currentUser.getUserId();
        department.setCreateBy(createBy);
        //执行业务
        Result result = departmentService.saveDeparment(department);
        return result;
    }

    @PutMapping("/updateDepartment")
    @BusLog(descrip = "修改部门状态")
    public Result updateUserState(@RequestBody Department department,
                                  @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即修改用户的用户id
        int updateBy = currentUser.getUserId();

        department.setCreateBy(updateBy);

        //执行业务
        Result result = departmentService.updateDepartment(department);

        //响应
        return result;
    }


    @DeleteMapping("/deleteDepartment/{departmentId}")
    @BusLog(descrip = "删除部门")
    public Result deleteUser(@PathVariable Integer departmentId) {
        //执行业务
        departmentService.deleteDepartmentById(departmentId);
        //响应
        return Result.ok("用户删除成功！");
    }


    @GetMapping("/department-info/{departmentId}")
    public Result getDepartmentIdInfo(@PathVariable Integer departmentId,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();
        return departmentService.searchById(departmentId);
    }

    @GetMapping("/departmentId-all")
    public Result DepartmentIdAll() {
        //执行业务
        List<Department> userList = departmentService.selectAll();
        //响应
        return Result.ok(userList);
    }

    @GetMapping("/department-list")
    public Result userListPage(Page page, Department department) {
        //执行业务
        page = departmentService.queryDepartmentPage(page, department);
        //响应
        return Result.ok(page);
    }

}
